﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using PSTS.Models;
using PSTS.UI.Views.Pages;
using PSTS.UI.Commands;

namespace PSTS.UI.ViewModels
{
    class MainWindowViewModel: DependencyObject
    {
        private TestChapterSelectionPageViewModel _testChapterSelectionPageViewModel;
        private TestChapterSelectionPage _testChapterSelectionPage;
        private CommandBinding _completeTestCommandBinding;
        public MainWindowViewModel()
        {
            _testChapterSelectionPageViewModel = new TestChapterSelectionPageViewModel();
            _testChapterSelectionPage = new TestChapterSelectionPage(_testChapterSelectionPageViewModel);
            _testChapterSelectionPage.CommandBindings.Add(new CommandBinding(
                PSTSCommands.StartTestPassingCommand,
                StartTestPassingCommand_Executed,
                StartTestPassingCommand_CanExecute));
            _completeTestCommandBinding = new CommandBinding(
                PSTSCommands.CompleteTestCommand,
                CompleteTestCommand_Executed,
                CompleteTestCommand_CanExecute);
            MainFrameContent = _testChapterSelectionPage;
        }



        #region properties

        public Page MainFrameContent
        {
            get { return (Page)this.GetValue(MainFrameContentProperty); }
            set { this.SetValue(MainFrameContentProperty, value); }
        }
        public static readonly DependencyProperty MainFrameContentProperty = DependencyProperty.Register(
          "MainFrameContent",
          typeof(Page),
          typeof(MainWindowViewModel),
          new PropertyMetadata(null));

        private TestPassingPage CurTestPassingPage { get; set; }
        #endregion



        #region commands handlers

        public void StartTestChapterSelectionCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //use constructor with paremeters, send TestChapterSelectionPageViewModel, initialise it before it
            _testChapterSelectionPageViewModel = new TestChapterSelectionPageViewModel();
            _testChapterSelectionPage = new TestChapterSelectionPage(_testChapterSelectionPageViewModel);
            _testChapterSelectionPage.CommandBindings.Add(new CommandBinding(
                PSTSCommands.StartTestPassingCommand,
                StartTestPassingCommand_Executed,
                StartTestPassingCommand_CanExecute));
            _completeTestCommandBinding = new CommandBinding(
                PSTSCommands.CompleteTestCommand,
                CompleteTestCommand_Executed,
                CompleteTestCommand_CanExecute);
            MainFrameContent = _testChapterSelectionPage;
        }

        public void StartTestChapterSelectionCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void StartTestPassingCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            int max =
                Repository.Instance.GetCollectionOfAllQuestionsForTopics(
                    _testChapterSelectionPageViewModel.SelectedTopics).Count;

            if (_testChapterSelectionPageViewModel.SelectAll)
            {
                _testChapterSelectionPageViewModel.QuestionsNumber = max;
            }

            if (max >= _testChapterSelectionPageViewModel.QuestionsNumber)
            {
                if (_testChapterSelectionPageViewModel.WithTimer)
                {
                    var xx = new DateTime(200, 1, 1, 1, 0, 0);
                    xx = xx.AddMinutes(_testChapterSelectionPageViewModel.MM);
                    xx = xx.AddSeconds(_testChapterSelectionPageViewModel.SS);
                    CurTestPassingPage =
                        new TestPassingPage(
                            new TestPassingPageViewModel(_testChapterSelectionPageViewModel.GetQuestions(), CompleteTest,
                                xx)
                            {CompleteTestCommandBinding = _completeTestCommandBinding});
                    MainFrameContent = CurTestPassingPage;
                }
                else
                {
                    CurTestPassingPage =
                       new TestPassingPage(
                           new TestPassingPageViewModel(_testChapterSelectionPageViewModel.GetQuestions(), CompleteTest)
                           { CompleteTestCommandBinding = _completeTestCommandBinding });
                    MainFrameContent = CurTestPassingPage;
                }
            }
            else
            {
                MessageBox.Show(string.Format("У вибраних темах є лише {0} питань.", new object[]{max.ToString()}),
                    "Недостатня кількість питань",
                    MessageBoxButton.OK, MessageBoxImage.Asterisk);
                _testChapterSelectionPageViewModel.QuestionsNumber = max;
            }
        }

        private void StartTestPassingCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (_testChapterSelectionPageViewModel.QuestionsNumber > 0 || _testChapterSelectionPageViewModel.SelectAll) &&
                _testChapterSelectionPageViewModel.SelectedTopics.Count > 0;
        }        
        
        public void LoginCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //use constructor with paremeters, send TestChapterSelectionPageViewModel, initialise it before it
            var viewModel = new LoginPageViewModel();
            viewModel.Verified += AdminPageCommand_Executed;
            MainFrameContent = new LoginPage(viewModel);
        }

        public void LoginCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        public void AdminPageCommand_Executed(object sender, EventArgs e)
        {
            //use constructor with paremeters, send TestChapterSelectionPageViewModel, initialise it before it
            var viewModel = new AdminPageViewModel();

            MainFrameContent = new AdminPage(viewModel);
        }

        public void AdminPageCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }


        private void CompleteTestCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CompleteTestCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var viewModel = CurTestPassingPage.DataContext as TestPassingPageViewModel;
            if (viewModel != null)
            {
                viewModel.ComleteTest();
                //foreach (var page in viewModel.QuestionPagesList)
                //{
                //    page.Confirm();
                //}
                
                //MainFrameContent = new ResultPage(new ResultPageViewModel(viewModel));

                //CurTestPassingPage = null;
            }
        }
        #endregion

        private void CompleteTest(Page resPage)
        {
            MainFrameContent = resPage;
            CurTestPassingPage = null;
        }
    }
}
